import React, { Component } from 'react'
import './index.css'
import { reqPushNews } from '../../../api'

export default class index extends Component {
  state = {
    isHidden: [],
    infos: [],
  }
  componentDidMount = async () => {
    const res = await reqPushNews()
    const infos = res.data
    const n = infos.length
    const isHidden = new Array(n).fill(true)
    this.setState({isHidden, infos})
  }
  handleHidden = (n) => {
    const isHidden = [...this.state.isHidden]
    isHidden[n] = !isHidden[n]
    this.setState({isHidden})
  }
  render() {
    const { isHidden, infos } = this.state
    return (
      <div className="info">
        <div className="i_items">
        {
          infos.map((info, index) => {
            return (
              <>
                <div className="i_item" onClick={() => this.handleHidden(index)}>
                  <h4 className="title">{info.title}</h4>
                  <span className="from">{info.from}</span>
                  <p className="content">{info.content}</p>
                  <span className="time">{info.date}</span>
                </div>
                <div className="i_detail" hidden={isHidden[index]}>
                  <h4 className="title">{info.title}</h4>
                  <div className="from">{info.from} {info.date}</div>
                  <p className="content">{info.content}</p>
                </div>
              </>
            )
          })
        }
        </div>
      </div>
    )
  }
}
